<?php
if( !function_exists('tpl_parse_ext')) {
	function tpl_parse_ext($template){
		return template_ext($template);
	}
}
//模板扩展函数
function template_ext($template){
	//转义列表
	$replaceList=array(
		'search' => array(
			//常量括式
			'/\{\#(\w+)\#\}/',	
			//常量下划线
			'/__[A-Z]+__/',
			//去除注释标签
			'/<!--{(.+?)}-->/',
			//判断
			'/{\if="([^"]*)"}/',
			'/{\elseif="([^"]*)"}/',
			'/\{else\}/',
			'/\{\/if\}/',
			//循环
			'/{\/loop}/',
		),
		'replace' => array(
			"<?php echo $1; ?>",
			"<?php echo $0; ?>",
			'{$1}',
			"<?php if($1) { ?>",
			'<?php }else if($1) { ?>',
			'<?php }else{ ?>',
			'<?php } ?>',
			'<?php }} ?>',

		)					   
	);
	$template = preg_replace( $replaceList['search'], $replaceList['replace'], $template);

	//解析循环标签
	if(preg_match_all('/{loop(?: name){0,1}="{0,1}([^"]*)"}/', $template, $matches)){
		foreach ($matches[1] as $k => $v) {
			$parameter=preg_replace("/\s+/"," ",$matches[1][$k]);
			$parameter=explode(' ',$parameter);
			$code='';
			if(count($parameter)<2){
				$parameter[1]='$key';
				$parameter[2]='$vo';
			}
			foreach ($parameter as $key => $value) {
				switch ($key) {
					case 0:
						$code.=$value;
						break;
					case 1:
						$code.=' as '.$value;
						$parameter2 = $value;
						break;
					case 2:
						$code.=' => '.$value;
						$parameter2 = $value;
						break;
				}			
			}

			$phpText = '<?php  if(isset('.$parameter[0].') && is_array('.$parameter[0].') && !empty('.$parameter[0].')){ '.$parameter2.'_i=0; foreach('.$code.'){ '.$parameter2.'_i++; '.$parameter2.'[i]='.$parameter2.'_i;  ?>';
			$template = str_replace($matches[0][$k], $phpText, $template);
		}
	}

	//转义变量 
	if (preg_match_all('/\{\$(\w+(?:\.\${0,1}[A-Za-z0-9_]+)*(?:(?:\[\${0,1}[A-Za-z0-9_]+\])|(?:\-\>\${0,1}[A-Za-z0-9_]+))*)(.*?)\}/',$template,$matches)){
	    for ($parsed = array(), $i = 0, $n = count($matches[0]); $i < $n; $i++)
            $parsed[$matches[0][$i]] = array('var' => $matches[1][$i], 'extraVar' => $matches[2][$i]); foreach ($parsed as $tag => $array) {
            $var = $array['var'];
            $extraVar = $array['extraVar'];
            //替换变量
            $newVar = '$'.preg_replace('/\.(\${0,1}\w+)/', '["\\1"]', $var);
            //是否为赋值
            $isVariable = preg_match("/^(\s*?)\=[^=](.*?)$/", $extraVar);
            //处理URL函数
            if($newVar=='$url'&&substr($extraVar,0,1) == '|'){
            	$url=substr($extraVar,1);
            	$newVar=$url;
            	$extraVar='|url';
            }
            //函数处理
            if(!empty($extraVar)&&substr($extraVar,0,1) == '|'){
            	$function = substr($extraVar,1);
            	$parameter = explode(':', $function,2);
            	$function = $parameter[0];
            	$parameter = $parameter[1];
            }else{
            	$function = null;
            }

            if($isVariable){
				$phpText = '<?php '.$newVar.$extraVar.'; ?>';
            }else if(!empty($function)){
            	if($parameter){
            		$phpText = '<?php echo '.$function.'('.$newVar.','.$parameter.'); ?>';
            	}else{
            		$phpText = '<?php echo '.$function.'('.$newVar.'); ?>';
            	}
            }else{
            	$phpText = '<?php echo '.$newVar.$extraVar.'; ?>';
            }
            $template = str_replace($tag, $phpText, $template);
        }

    }
	return $template;

}